package com.wanho.java159.cb.controller;

import com.github.pagehelper.PageInfo;
import com.wanho.java159.common.result.R;
import com.wanho.java159.cb.po.Order;
import com.wanho.java159.cb.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Order 控制层
* @author ChaoFei.Zhang
* @date   2021-07-10
*/
@Api(tags = "管理Order")
@CrossOrigin
@RestController
@RequestMapping("/admin/order")
public class OrderController {
    /**Order业务层对象 */
    @Resource
    private OrderService orderService;

    /**
    * 查询所有Order
    * @return 封装的:list 所有的Order数据
    */
    @ApiOperation("列表查询Order")
    @GetMapping("/list")
    public R listAll(){
        return R.ok().data("list",orderService.list());
    }


    /**
    * 根据id删除Order
    * @param id Order的主键
    * @return ok 删除成功  error 删除失败
    */
    @ApiOperation(value = "根据id删除Order", notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable("id") @ApiParam(value = "数据id", required = true, example = "100") Long id){
        boolean remove = orderService.remove(id);
        if (remove){
        return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    /**
    * 新增一条Order记录
    * @param order 封装新增的Order数据
    * @return ok 新增成功  error 新增失败
    */
    @ApiOperation(value = "新增Order", notes = "新增Order")
    @PostMapping("/save")
    public R save(@ApiParam(value = "封装新增Order对象", required = true) @RequestBody Order order){
        boolean remove = orderService.save(order) ;
        if (remove){
            return R.ok().message("新增成功");
        }
        return R.error().message("新增失败");
    }

    /**
    * 根据id查询一条Order记录
    * @param id Order的主键
    * @return 封装 record 存储 Order对象
    */
    @ApiOperation(value = "根据id查询Order", notes = "根据id查询Order")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") @ApiParam(value = "数据id", required = true, example = "100") Long id){
        return R.ok().data("record",orderService.get(id)) ;
    }

    /**
    * 修改一条Order记录
    * @param order 封装Order对象
    * @return ok 修改成功  error 修改失败
    */
    @ApiOperation(value = "修改Order", notes = "修改Order")
    @PutMapping("/update")
    public R update(@ApiParam(value = "封装修改Order对象", required = true) @RequestBody Order order){
        boolean remove = orderService.update(order) ;
        if (remove){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    /**
    * 分页条件查询Order记录
    * @param page 页码
    * @param limit 分页跨度
    * @param order 封装Order查询条件
    * @return pageModel 封装分页对象
    */
    @ApiOperation("获取Order分页列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
        @ApiParam(value = "当前页码", required = true)
        @PathVariable Long page,
        @ApiParam(value = "每页记录数", required = true)
        @PathVariable Long limit,
        @ApiParam(value = "查询对象", required = false)
        Order order) {
        PageInfo<Order> pageModel =  orderService.listPage(page,limit, order);
        return R.ok().data("pageModel", pageModel);
    }
}